const http= require('http');
const fs= require('fs');
const path= require('path');
var mimeModule=require('./model/getmime')
 
http.createServer(function (req, res) {
    //使用path模块中的extname方法获取文件后缀名
    

    let pathname=req.url;
    if(pathname=='/'){
        pathname='index.html'
        
    }
    var extname=path.extname(pathname) 
    if (pathname!=='./favicon.ico') {
        fs.readFile('./static/'+pathname,(err,data)=>{

            if(err){
                fs.readFile('./static/404.html',(err,data_404)=>{                    
                    res.writeHead(404, {'Content-Type': 'text/html;charset="utf-8"'});
                    res.write(data_404)
                    res.end();
                });
                return;
            }else{
                var mime=mimeModule.getMime(extname); 
                res.writeHead(200, {'Content-Type': ''+mime+';charset="utf-8"'});
                res.write(data)
                res.end();
            }
            
        })
    }
  

}).listen(8081);